package org.zk.dubbo_consumer.utils;


import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName: AliPlayAndPushUtils
 * @Description： 阿里云直播推流拉流地址工具类
 * @Author: 刘艺广
 * @Date: 2019/12/12 0012
 **/
public class AliPlayAndPushUtil {
    /**
     * 推流域名 阿里云配置的推流域名
     */
    private static final String pushDomain = "push.alicelonely.com";
    /**
     * 拉流域名 阿里云配置的拉流域名
     */
    private static final String pullDomain = "pull.alicelonely.com";


    /**
     * 鉴权key
     */
    private static final String pullKey = "X72T74TY98";


    private static final String pushKey = "0sjUbDYrCg";

    /**
     * @MethodName: CreateServer
     * @Description: 给主播创建推流地址中的服务器
     * @Param: [streamName]
     * @Return: java.lang.String 服务器地址
     * @Author: 刘艺广
     * @Time: 2019/12/12 0012
     **/

    public static String CreateServer(String appName) {
        String server = "rtmp://" + pushDomain + "/" + appName;
        return server;
    }

    /**
     * @MethodName: CreateSecretKey
     * @Description: 创建直播的串流密钥
     * @Param: [appName, streamName]
     * @Return: java.lang.String
     * @Author: 刘艺广
     * @Time: 2019/12/19 0019 上午 9:25
     **/

    public static String CreateSecretKey(String appName,String streamName){
         Long time = System.currentTimeMillis() / 1000;
         String strpush = "/" + appName + "/" + streamName + "-" + time + "-0-0-" + pushKey;
         String secretKey=streamName + "?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strpush);
         return secretKey;
    }


    /**
     * @MethodName: GetPlayUrl
     * @Description: 获得拉流地址的方法：原画，flv,m3u8  本次直播采用flv格式，并对其进行转码
     * @Param: [appName, streamName]
     * @Return: java.lang.String
     * @Author: 刘艺广
     * @Time: 2019/12/19 0019 上午 11:04
     **/

    public static Map GetPlayUrl(String appName,String streamName) {
        Long time = System.currentTimeMillis() / 1000;

        String strViewRtmp = "/" + appName + "/" + streamName + "-" + time + "-0-0-" + pullKey;
        String strFlvSD = "/" + appName + "/" + streamName + "_sd.flv-" + time + "-0-0-" + pullKey;   //flv转码sd的签名字符串
        String strFlvHD = "/" + appName + "/" + streamName + "_hd.flv-" + time + "-0-0-" + pullKey;   //flv转码hd的签名字符串
        String strFlvUD = "/" + appName + "/" + streamName + "_ud.flv-" + time + "-0-0-" + pullKey;   //flv转码ud的签名字符串
        String strViewM3u8 = "/" + appName + "/" + streamName + ".m3u8-" + time + "-0-0-" + pullKey;

        String rtmpUrl = "rtmp://" + pullDomain + "/" + appName + "/" + streamName + "?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strViewRtmp);
        String flvUrlSD = "http://" + pullDomain + "/" + appName + "/" + streamName + "_sd.flv?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strFlvSD);//flv转码sd的拉流地址
        String flvUrlHD = "http://" + pullDomain + "/" + appName + "/" + streamName + "_hd.flv?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strFlvHD);//flv转码hd的拉流地址
        String flvUrlUD = "http://" + pullDomain + "/" + appName + "/" + streamName + "_ud.flv?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strFlvUD);//flv转码ud的拉流地址
        String m3u8url = "http://" + pullDomain + "/" + appName + "/" + streamName + ".m3u8?auth_key=" + time + "-0-0-" + MD5Util.getMD5(strViewM3u8);

        Map<String, String> pullUrlMap = new HashMap<>(5);
        pullUrlMap.put("rtm", rtmpUrl);
        pullUrlMap.put("flvUrlSD", flvUrlSD);
        pullUrlMap.put("flvUrlHD", flvUrlHD);
        pullUrlMap.put("flvUrlUD", flvUrlUD);
        pullUrlMap.put("m3u8", m3u8url);
        System.out.println("原画:    " + rtmpUrl);
        System.out.println("flv转码sd:    " + flvUrlSD);
        System.out.println("flv转码hd:    " + flvUrlHD);
        System.out.println("flv转码ud:    " + flvUrlUD);
        System.out.println("m3u8:    " + m3u8url);
        return pullUrlMap;
    }

}

